package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"user-mgmt-sys/cache"
	"user-mgmt-sys/model"
	"user-mgmt-sys/pkg"
)

func ChenLogin(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "chen_login_" + username
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if cacheData == 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误三次禁止登录",
		})
		return
	}
	var user model.User
	err := model.DB.Where("username = ?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码不正确",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号不存在",
		})
		return
	}
	if user.Password != pkg.Md5str(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if cacheData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})
	return
}
func ChenRoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func ChenUserRole(c *gin.Context) {
	uid := c.Query("uid")
	//SELECT
	//*
	//	FROM
	//roles
	//LEFT JOIN user_roles ON user_roles.rid=roles.id
	//WHERE user_roles.uid=1
	var role []model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid=roles.id").
		Where("user_roles.uid = ?", uid).
		Debug().
		Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": role,
	})
	return
}
func ChenRoleNodes(c *gin.Context) {
	rid := c.Query("rid")
	//SELECT
	//*
	//	FROM
	//nodes
	//LEFT JOIN role_nodes ON nodes.id=role_nodes.nid
	//WHERE role_nodes.rid=1
	var nodes []model.Node
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id=role_nodes.nid").Where("role_nodes.rid = ?", rid).
		Debug().
		Find(&nodes)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": nodes,
	})
	return
}
func Chen(c *gin.Context) {
	title := c.PostForm("title")
	price, _ := strconv.Atoi(c.PostForm("price"))
	li := c.PostForm("li")
	if title == "" || price == 0 || li == "" {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.Chen{
		Title: title,
		Price: price,
		Li:    li,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func ChenAdd(c *gin.Context) {
	title := c.PostForm("title")
	price, _ := strconv.Atoi(c.PostForm("price"))
	li := c.PostForm("li")
	bian, _ := strconv.Atoi(c.PostForm("bian"))
	name := c.PostForm("name")
	chen := c.PostForm("chen")
	prices, _ := strconv.Atoi(c.PostForm("prices"))
	if title == "" || price == 0 || li == "" || bian == 0 || name == "" || chen == "" || prices == 0 {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.ChenDinglist{
		Title:  title,
		Price:  price,
		Li:     li,
		Bian:   bian,
		Name:   name,
		Chen:   chen,
		Prices: prices,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func ChenList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var list []model.ChenDinglist
	//SELECT
	//chens.title AS Title,
	//	chens.price AS Price,
	//	chens.li AS Li,
	//	dings.bian AS Bian,
	//	dings.name AS Name,
	//	dings.prices as PRices
	//FROM
	//dings
	//LEFT JOIN chens ON dings.cid=chens.id
	redis_key := "chen_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("dings").
			Select(
				"dings.cid as DingId",
				"	chens.title AS Title",
				"chens.price AS Price",
				"chens.li AS Li",
				"dings.bian AS Bian",
				"dings.name AS Name",
				"dings.prices as Prices",
				"dings.chen AS Chen",
			).
			Joins("LEFT JOIN chens ON dings.cid=chens.id").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var count int64
	model.DB.Model(&model.Ding{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"list":  list,
			"count": count,
		},
	})
	return

}
func ChenDetail(c *gin.Context) {
	id := c.PostForm("id")
	var list model.ChenDinglist
	redis_key := "chen_list_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("dings").
			Select(
				"dings.cid as DingId",
				"chens.title AS Title",
				"chens.price AS Price",
				"chens.li AS Li",
				"dings.bian AS Bian",
				"dings.name AS Name",
				"dings.prices as Prices",
				"dings.chen AS Chen",
			).
			Joins("LEFT JOIN chens ON dings.cid=chens.id").
			Where("dings.id=?", id).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
